package zzz.pic_img;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {

    public static void main(String[] args) {
        String path = "C:\\Users\\Administrator\\Desktop\\pic.jpg";
        String out = "C:\\Users\\Administrator\\Desktop\\pic.png";
        int imgLeftPadding = 25;
        int imgTopPadding = 76;
        int imgRightPadding = 230;
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File(path));
        } catch (IOException e) {
            e.printStackTrace();
        }
        int height = img.getHeight();
        int width = img.getWidth();
        int[][] data = new int[width][height];
        for (int x = 0; x < data.length; x++) {
            for (int y = 0; y < data[x].length; y++) {
                int rgb = img.getRGB(x, y);
                data[x][y] = rgb;
            }
        }
        int nn = 37;
        int xs = imgLeftPadding;
        int ys = imgTopPadding;
        double kk = (double) (imgRightPadding - xs) / (double) nn;
        int kkn = (int) kk;
        int[][] datat = new int[nn][nn];
        int[][] dataz = new int[nn][nn];
        for (int i = 0; i < nn; i++) {
            int x1 = (int) Math.round(xs + kk * i);
            for (int j = 0; j < nn; j++) {
                int y1 = (int) Math.round(ys + kk * j);
                Color color = ccc(x1, y1, kkn, kkn, data);
                datat[i][j] = color.getRGB();
                Color colorz = cccz(x1, y1, kkn, kkn, data);
                dataz[i][j] = colorz.getRGB();
            }
        }
        datat = conv(datat, dataz);
        print(datat);
        create(datat, nn, nn, out);
    }

    private static void create(int[][] datat, int nn, int nn2, String path) {
        BufferedImage bi = new BufferedImage(nn * 10, nn2 * 10, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = bi.getGraphics();
        for (int i = 0; i < datat.length; i++) {
            for (int j = 0; j < datat[i].length; j++) {
                int cc = datat[i][j];
                if (cc == 0) {
                    graphics.setColor(Color.WHITE);
                }
                if (cc == 1) {
                    graphics.setColor(Color.BLACK);
                }
                if (cc == 2) {
                    graphics.setColor(Color.GREEN);
                }
                graphics.fillRect(i * 10, j * 10, 10, 10);
            }
        }
        File output = new File(path);
        try {
            ImageIO.write(bi, "png", output);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int[][] conv(int[][] datat, int[][] dataz) {
        int nn = datat.length;
        int[][] result = new int[nn][nn];
        for (int i = 0; i < datat.length; i++) {
            for (int j = 0; j < datat[i].length; j++) {
                int color = datat[i][j];
                Color color2 = new Color(color);
                int argv = argv(color2);
                if (argv == 2) {
                    int colorz = dataz[i][j];
                    Color colorz2 = new Color(colorz);
                    int argvz = argv(colorz2);
                    result[i][j] = argvz;
                } else {
                    result[i][j] = argv;
                }
            }
        }
        return result;
    }

    private static int argv(Color color) {
        int red = color.getRed();
        int green = color.getGreen();
        int blue = color.getBlue();
        int min = 50;
        int max = 256 - 50;
        if (red < min && green < min && blue < min) {
            return 1;
        } else if (red > max && green > max && blue > max) {
            return 0;
        } else {
            return 2;
        }
    }

    private static void print(int[][] datat) {
        for (int j = 0; j < datat.length; j++) {
            for (int i = 0; i < datat[j].length; i++) {
                System.out.print("\t" + datat[i][j]);
            }
            System.out.println();
        }
    }

    private static Color ccc(int x1, int y1, int w, int h, int[][] data) {
        int count = 0;
        int redSum = 0;
        int greenSum = 0;
        int blueSum = 0;
        int redArgv = 0;
        int greenArgv = 0;
        int blueArgv = 0;

        for (int i = 1; i < w - 1; i++) {
            for (int j = 1; j < h - 1; j++) {
                int cur = data[i + x1][j + y1];
                Color color = new Color(cur);
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                redSum += red;
                greenSum += green;
                blueSum += blue;
                count++;
            }
        }
        redArgv = (int) Math.round(redSum / count);
        greenArgv = (int) Math.round(greenSum / count);
        blueArgv = (int) Math.round(blueSum / count);
        return new Color(redArgv, greenArgv, blueArgv);
    }

    private static Color cccz(int x1, int y1, int w, int h, int[][] data) {
        int count = 0;
        int redSum = 0;
        int greenSum = 0;
        int blueSum = 0;
        int redArgv = 0;
        int greenArgv = 0;
        int blueArgv = 0;

        for (int i = 2; i < w - 2; i++) {
            for (int j = 2; j < h - 2; j++) {
                int cur = data[i + x1][j + y1];
                Color color = new Color(cur);
                int red = color.getRed();
                int green = color.getGreen();
                int blue = color.getBlue();
                redSum += red;
                greenSum += green;
                blueSum += blue;
                count++;
            }
        }
        redArgv = (int) Math.round(redSum / count);
        greenArgv = (int) Math.round(greenSum / count);
        blueArgv = (int) Math.round(blueSum / count);
        return new Color(redArgv, greenArgv, blueArgv);
    }
}
